<template>
  <div>
    <el-form
      :inline="true"
      :model="formInline"
      class="demo-form-inline"
      style="margin: 20px 30px 0px"
    >
      <el-form-item>
        <el-button type="primary" @click="handleQuery()">查询</el-button>
        <el-button type="primary" @click="addCategory()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="level1"
      style="width: 100%"
      row-key="id"
      :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
    >
      <el-table-column label="名称" width="300">
        <template #default="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>

      <el-table-column label="父分类" width="250">
        <template #default="scope">
          {{ scope.row.parent }}
        </template>
      </el-table-column>

      <el-table-column label="顺序" width="250">
        <template #default="scope">
          {{ scope.row.sort }}
        </template>
      </el-table-column>

      <el-table-column label="操作" width="200">
        <template #default="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >&nbsp;&nbsp;
          <el-dialog
            title="分类表单"
            :width="500"
            v-model="dialogFormVisible"
            :modal="false"
          >
            <el-form :model="category" v-loading="loading">
              <el-form-item label="名称" :label-width="formLabelWidth">
                <el-input v-model="category.name" autocomplete="off"></el-input>
              </el-form-item>
              <el-form-item label="父分类" :label-width="formLabelWidth">
                <el-input v-model="category.parent" autocomplete="off"></el-input>
                <el-select v-model="category.parent" placeholder="请选择">
                  <el-option value="0">无</el-option>
                  <el-option
                    v-for="c in level1"
                    :key="c.id"
                    :label="c.name"
                    :value="c.id"
                    :disabled="c.id===this.category.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="顺序" :label-width="formLabelWidth">
                <el-input v-model="category.sort" autocomplete="off"></el-input>
              </el-form-item>
            </el-form>
            <template #footer>
              <span class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="handleSave">确 认</el-button>
              </span>
            </template>
          </el-dialog>

          <el-popconfirm
            confirmButtonText="确认"
            cancelButtonText="取消"
            icon="el-icon-info"
            iconColor="red"
            title="删除后不可恢复，确认删除?"
            @confirm="handleDelete(scope.$index, scope.row)"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import axios from "axios";
import { ref, reactive } from "vue";
import { defineComponent } from "vue";
import { ElMessage } from "element-plus";
import Tool from "../../util/tool"
export default {
  name: "AdminCategory",
  data() {
    return {
      dialogFormVisible: false,
      category: "",
      formLabelWidth: "120px",
      formComponentSize: "100px",
      loading: false,
    };
  },
  setup() {
    console.log("组件初始化成功");
    const categorys = ref();

    const formInline = ref();
    formInline.value = {};

    const level1=ref();
    /**
     * 数据查询
     **/
    const handleQuery = () => {
      let _this = this;
      axios
        .get("/category/all")
        .then(function (response) {
          const data = response.data;
          if (data.success) {
            categorys.value = data.content;
            console.log("原始数组：",categorys.value)

            level1.value=[];
            level1.value=Tool.array2Tree(categorys.value,0)
          } else {
            ElMessage.error(data.message);
          }
        })
        .catch(function (error) {
          console.log(error);
        });
    };

    onMounted: {
      console.log("onMounted=>2222");
      handleQuery();
    }

    return {
      level1,
      //categorys,
      handleQuery,
      formInline,
    };
  },
  methods: {
    handleEdit(index, row) {
      console.log("index:" + index);
      console.log("row", row);
      this.dialogFormVisible = true;
      this.category = Tool.copy(row);
    },
    handleDelete(index, row) {
      console.log("==========删除==========");
      console.log("index:" + index);
      console.log("row", row);
      const _this = this;
      _this.axios
        .delete("/category/delete/" + row.id)
        .then(function (response) {
          const data = response.data;
          if (data.success) {
            ElMessage.success({
              message: "操作成功！",
              type: "success",
            });

            //重新加载列表
            _this.handleQuery();
          }
        })
        .catch(function (error) {
          ElMessage.error("操作失败！");
          console.log(error);
        });
    },
    handleSave() {
      this.loading = true;
      const _this = this;
      _this.axios
        .post("/category/save", _this.category)
        .then(function (response) {
          _this.loading = false;
          const data = response.data;
          if (data.success) {
            ElMessage.success({
              message: "操作成功！",
              type: "success",
            });
            _this.dialogFormVisible = false;

            //重新加载列表
            _this.handleQuery();
          } else {
            ElMessage.error(data.message);
          }
        })
        .catch(function (error) {
          _this.loading = false;
          _this.dialogFormVisible = false;
          ElMessage.error("操作失败！");
          console.log(error);
        });
    },
    onSubmit() {
      console.log("submit!");
    },
    addCategory() {
      this.dialogFormVisible = true;
      this.category = {};
    },
  },
};
</script>

<style>
</style>
